package com.dazhi.show.core;

import com.dazhi.show.config.MyConfig;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Component
public class UploadFileCacheCopyUtil {
    @Resource
    private MyConfig myConfig;
    public boolean copyTo(String name, String path) throws IOException {
        String baseFolder = myConfig.baseFolder;
        String tempFolder = myConfig.tempFolder;
        // 判断临时文件是否存在
        File tempFile = new File(tempFolder + name);
        File dirFolder = new File(baseFolder + path.substring(0, path.lastIndexOf("/")));
        if (!dirFolder.exists()) {
            dirFolder.mkdirs();
        }
        File newFile = new File(baseFolder + path);
        if (tempFile.exists()) {
            // 将临时文件复制到指定文件夹中
            FileCopyUtils.copy(tempFile,newFile);
            return true;
        }else {
            return false;
        }
    }
}
